""" ****************************************************************
 Module: meterparse.py
 ver 0.9.0; Phase I

 Rev 0.9.2; Last: 03/11/2013 - Previous: 03/11/2013 (AEM)
   - Incorporate Plot DMM modules
 
 Rev 0.9.3-0; Last: 03/24/2013 - Previous: 03/22/2013 (AEM)
   - Mods to accommodate GUI (module readermain.py)
     Either execution mode is transparent to this module.

 Support functions for readmeter.py

 Parse and interpret meter reading results.
 Send back:
  - reading value (AC/DV volts, AC/DC amps, ohms, temperature C/F)
  - scale (milli, K, M)

 This file is part of the DMM Reader project.

 Copyright (C) 2012-2013  Arnold E. Morgan

    DMM Reader is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""

def GetMeasType(MeterReading):
   """ Parse the meter reading and determine:
   - Meter setting, Reading type, Polarity, Value measured
   Separate functions will parse out the various reading types.

   Receive: Raw reading from the meter
   Returns:
      RtnStat - False = Fail, True = Success
      Value Type - '', 'TEMP', 'DCV', 'ACV', 'OHM', 'DCA', 'ACA'
      Polarity - '-', ''
      Measurement Value - the floating point number returned from the meter
      Multipler; '', 'M' = Meg, 'k' - kilo, 'm' = milli
   """
   
   RtnValue = (False, '', '', '', '')
   
   DialSetting = MeterReading[:2]
   # Looking for 'TE', 'DC', 'OH', 'AC'
   
   ReadingUnits = MeterReading[10:].strip()
   # Looking for 'C', 'V', 'Ohm', 'mA'
   
   XMultiplier = MeterReading[9]
   
   ReadingValue = MeterReading[4:9].strip()
   # Value will be an int or floating point depending on type
   
   ReadingSign = MeterReading[3]
   # Will be '-' or ' '
   
   if DialSetting == "TE" and ReadingUnits == "C":
      RtnValue = (True, "TEMP", ReadingSign, ReadingValue, "")
   elif DialSetting == "OH" and ReadingUnits == "Ohm":
      RtnValue = (True, "OHM", '', ReadingValue, XMultiplier)
   elif DialSetting == "DC" and ReadingUnits == "V":
      RtnValue = (True, "DCV", ReadingSign, ReadingValue, XMultiplier)
   elif DialSetting == "DC" and (ReadingUnits == "mA" or ReadingUnits == "A"):
      RtnValue = (True, "DCA", ReadingSign, ReadingValue, ReadingUnits)
   elif DialSetting == "AC" and (ReadingUnits == "mA" or ReadingUnits == "A"):
      RtnValue = (True, "ACA", ReadingSign, ReadingValue, ReadingUnits)
   elif DialSetting == "AC" and ReadingUnits == "V":
      RtnValue = (True, "ACV", ReadingSign, ReadingValue, ReadingUnits)
       
   return RtnValue
# ************** End of fcn GetMeasType *******************

# *** These next functions handle the various meter value types
# *** The main activity here is to build the console and log
#     output strings. This will keep the calling routines 
#     independent of the how the meter is actually being used.

def ConvertTempReading(SignValue, theValue):
   CTemp = int(theValue)
   FTemp = (CTemp * 1.8) + 32.0

   LogOutputStr = "%s %s" % (CTemp, FTemp)
   StdOutputStr = "Current Temp: %s C, %s F" % (CTemp, FTemp)

   return (str(CTemp), 'C', StdOutputStr, LogOutputStr) 
# ************** End of fcn ConvertTempReading *******************

def ConvertOhmReading(theValue, theXMult):
   if theValue == "O.L":
      OhmValue = -9999
   else:
      OhmValue = float(theValue)
   
   if theXMult == "k":
      OhmValue = OhmValue * 1000
   elif theXMult == "M":
      OhmValue = OhmValue * 1000000

   LogOutputStr = "%s" % OhmValue
   StdOutputStr = "%s" % OhmValue

   return (OhmValue, 'ohms', StdOutputStr, LogOutputStr) 
# ************** End of fcn ConvertOhmReading *******************

def ConvertDCVoltReading(theSign, theValue):
   if theValue == "O.L":
      VoltValue = 9999
   else:
      VoltValue = float(theValue)
      
   if theSign == "-":
      VoltValue = VoltValue * -1
   
   LogOutputStr = "%s" % VoltValue
   StdOutputStr = "%s" % VoltValue

   return (VoltValue, 'VDC', StdOutputStr, LogOutputStr) 
# ************** End of fcn ConvertDCVoltReading *******************

def ConvertACVoltReading(theValue):
   if theValue == "O.L":
      VoltValue = 9999
   else:
      VoltValue = float(theValue)
      
   LogOutputStr = "%s" % VoltValue
   StdOutputStr = "%s" % VoltValue

   return (VoltValue, 'VAC', StdOutputStr, LogOutputStr) 
# ************** End of fcn ConvertACVoltReading *******************

def ConvertDCAmpReading(theSign, theValue, theMult):
   if theValue == "O.L":
      AmpValue = 9999
   else:
      AmpValue = float(theValue)
      if theMult == "mA":
         AmpValue = AmpValue/1000
      
   if theSign == "-":
      AmpValue = AmpValue * -1
   
   LogOutputStr = "%s" % AmpValue
   StdOutputStr = "%s" % AmpValue

   return (AmpValue, 'ADC', StdOutputStr, LogOutputStr) 
# ************** End of fcn ConvertDCAmpReading *******************

def ConvertACAmpReading(theSign, theValue, theMult):
   if theValue == "O.L":
      AmpValue = 9999
   else:
      AmpValue = float(theValue)
      if theMult == "mA":
         AmpValue = AmpValue/1000
      
   if theSign == "-":
      AmpValue = AmpValue * -1
   
   LogOutputStr = "%2.2f" % AmpValue
   StdOutputStr = "%2.2f" % AmpValue

   return (AmpValue, 'AAC', StdOutputStr, LogOutputStr)
# ************** End of fcn ConvertACAmpReading *******************
